import { Component, Injector } from '@angular/core';
import { IonicPage, NavController, App, NavParams } from 'ionic-angular';
import { NativeService } from "../../../providers/NativeService";
import { FileService } from "../../../providers/FileService";
import { BasePage } from '../../basePage';
import { StorageService } from './../../../providers/StorageService';
import { TabsPage } from '../../tabs/tabs';
import { FeedbackPage } from '../../feedback/feedback'
import { UserService } from "./../../../providers/UserService"
import { UploadServiceProvider } from '../../../providers/upload-service/upload-service';
import { AppVersion} from '@ionic-native/app-version'

/**
 * Generated class for the SettingPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-setting',
  templateUrl: 'setting.html',
})
export class SettingPage extends BasePage {

  fileObjList = [];
  version:any   //当前版本
  app_url:any;  //更新地址

  constructor(injector: Injector, private app:App, public storage: StorageService,public userService:UserService,public usp:UploadServiceProvider,public appversion:AppVersion) {
    super(injector);
    
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SettingPage');
  }


  dismissCLI() {
    this.navCtrl.pop();
  }

  update() {
    this.getVersionNumber();
  }
  getVersionNumber(): Promise<string> {
    return new Promise((resolve) => {
      this.appversion.getVersionNumber().then((value: string) => {
        resolve(value);
        console.log(value)
        this.getLatesd(value);
      }).catch(err => {
        console.log('getVersionNumber:' + err);
      });
    });
  }

  //获取最新版本
  async getLatesd(i){
    let param={app_version:i}
    let result=await this.userService.checkVersionAPI(param);
    this.version=result['new_version'];
    this.app_url=result['app_url'];
    if(this.version > i){
      let alert = this.alertCtrl.create({
        title: '最新版本'+this.version+'发布啦，小仙女们快去看看吧',
        message: '老版本：'+i,
        buttons: [
          {
            text: '暂不更新',
            role: 'cancel',
            handler: () => {
              console.log('Cancel clicked');
            }
          },
          {
            text: '下载新版本',
            handler: () => {
              this.usp.downloadApp(this.app_url);
            }
          }
        ]
      });
      alert.present();
    }else{
      this.showAlert("暂无更新")
    }
  }

  nofunction() {
    let modal = this.modalCtrl.create('FeedbackPage');
    modal.onDidDismiss(data => {
    });
    modal.present();

  }

  /**文本框修改
  * type:文本框类型
  * inputname：文本框的名称
  * placeholder：文本框的字段名
  * fontnum:文本框文字显示的个数
  * inputvalue:需要回填回去的值
  * 文本框打开方式
  * InputCLI('text','昵称','nickname',10)
  * this.openTextArea('nihao','姓名',data=>{
     
   });
  */
  InputCLI(type, inputname, placeholder, fontnum) {
    let inputvalue = '张三';//后台获取的值
    this.openInputText(type, inputname, placeholder, fontnum, inputvalue, data => {

    });
  }


  /**提醒设置 */
  txsettingCLI() {
    let modal = this.modalCtrl.create('TxsettingPage');
    modal.onDidDismiss(data => {

    });
    modal.present();
  }


  /**关于我们 */
  aboutmeCLI() {
    let modal = this.modalCtrl.create('AboutmePage');
    modal.onDidDismiss(data => {

    });
    modal.present();
  }

  /**退出登录 */
  loginout() {
    this.storage.clear();
    // this.viewCtrl.dismiss();
    // this.navCtrl.popToRoot();
    // this.navCtrl.setRoot(TabsPage);
    // let modal = this.modalCtrl.create("LoginPage");
    // modal.onDidDismiss(data => { });
    // modal.present();


    // this.navCtrl.push("LoginPage")


    this.alertCtrl.create({
      title: '确认退出登录？',
      buttons: [{ text: '取消' },
      {
        text: '确定',
        handler: () => {
          const modal = this.modalCtrl.create('LoginPage');
          modal.present();
          modal.onDidDismiss(userInfo => {
           
          });
        }
      }
      ]
    }).present();

  }

  /*获取最新版本号*/
  async checkVersionAPI(){

  }

}
